package com.xiaoma.tpo.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.xiaoma.tpo.domain.User;

public class TPOUtils {
    
    private static Date maxDate;
    private static Date expiredDate;

    public static boolean over7days(Date date1, Date date2) {
        long m1 = date1.getTime();
        long m2 = date2.getTime();
        if((m1 - m2) > 86400000 * 7){
            return true;
        }
        return false;
    }
    
    public static Date getExpiredDate() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = "1900-1-1";
        if (expiredDate == null) {
            try {
                expiredDate = format.parse(dateStr);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return expiredDate;
    }
    
    public static Date getMaxDate() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = "9999-1-1";
        if (maxDate == null) {
            try {
                maxDate = format.parse(dateStr);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return maxDate;
    }
    
    public static boolean isAnonymous(User user) {
        if (user.getLastVisit().compareTo(getMaxDate()) != 0) {
            return false;
        }
        return true;
    }
    
    public static String get7DaysAgo() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(calendar.getTime());
    }
    
    
    public static int compareDate(Date date1, Date date2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date d1 = calendar.getTime();
        calendar.setTime(date2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date d2 = calendar.getTime();
        return d1.compareTo(d2);
    }
    
    public static Date parseDateFromString(String date) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(date);
    }
    
    public static Date[] getDateRange(int type){
        Date[] dates = new Date[2];
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if(1 == type){//天
            dates[0] = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            dates[1] = calendar.getTime();
        } else if(2 == type){//周
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            dates[0] = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            dates[1] = calendar.getTime();
        } else {//月
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            dates[0] = calendar.getTime();
            calendar.add(Calendar.MONTH, 1);
            dates[1] = calendar.getTime();
            
        }
        return dates;
    }
    
    public static Date[] getSpecificDateRange(int type, Date date){
        Date[] dates = new Date[2];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if(1 == type){//天
            dates[0] = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            dates[1] = calendar.getTime();
        } else if(2 == type){//周
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            dates[0] = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            dates[1] = calendar.getTime();
        } else {//月
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            dates[0] = calendar.getTime();
            calendar.add(Calendar.MONTH, 1);
            dates[1] = calendar.getTime();
            
        }
        return dates;
    }
    
    public static String format2Date(Date date) {
        return format(date, "yyyy-MM-dd");
    }
    public static String format2DateTime(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }
    
    private static String format(Date date, String format){
        return new SimpleDateFormat(format).format(date);
    }
    
    public static void main(String[] args) throws ParseException {
        Date specificDay = TPOUtils.parseDateFromString("2015-09-07");
        System.out.println(compareDate(specificDay, new Date()));
    }
}
